﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using OpenXLive.Silverlight;
using OpenXLive;

namespace MushroomPicker
{
    public partial class App : Application
    {
        public PhoneApplicationFrame RootFrame { get; private set; }

        private  string APISecretKey = "ns6wqvUQeSft3VKCxPjH9kRr ";

        public App()
        {
            UnhandledException += Application_UnhandledException;

            InitializeComponent();

            InitializePhoneApplication();

            if (System.Diagnostics.Debugger.IsAttached)
            {
                Application.Current.Host.Settings.EnableFrameRateCounter = true;
                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }

        }       

        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
           XLiveSLFormManager manager = new XLiveSLFormManager(this, APISecretKey);
            XLiveSLFormManager.BackgroundImage = "/MushroomPicker;component/OpenXLive.jpg";

            StartupPage.NewGameButtonClick += new EventHandler(StartupPage_NewGameButtonClick);
            StartupPage.AboutButtonClick += new EventHandler(StartupPage_AboutButtonClick);

            StartupPage.LeaderboardButton = false;

            manager.OpenSession();

            //Set the pages of OpenXLive is portrait.
            XLiveSLFormManager.Orientation = PageOrientation.Landscape;
            XLiveSLFormManager.SupportedOrientation = SupportedPageOrientation.Portrait;
        }
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            XLiveGameManager.Activated();

            StartupPage.NewGameButtonClick += new EventHandler(StartupPage_NewGameButtonClick);
            StartupPage.AboutButtonClick += new EventHandler(StartupPage_AboutButtonClick);

            StartupPage.LeaderboardButton = false;

            //Set the pages of OpenXLive is portrait.
            XLiveSLFormManager.Orientation = PageOrientation.Landscape;
            XLiveSLFormManager.SupportedOrientation = SupportedPageOrientation.Portrait;
        }
        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
            XLiveGameManager.Deactivated();
        }

        private void Application_Closing(object sender, ClosingEventArgs e)
        {
        }

        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        void StartupPage_AboutButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("OpenXLive Demo 1.0");
        }

        void StartupPage_NewGameButtonClick(object sender, EventArgs e)
        {
            this.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        #region Phone application initialization

        private bool phoneApplicationInitialized = false;

        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;

            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            phoneApplicationInitialized = true;
        }

        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;

            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        #endregion
    }
}